export default {

	/* 
	 * 将cityNo 转 cityName
	 * cityData:原数据
	 * cityNo：二级地区编码
	 */
	getCityName(cityData, cityNo) {
		if (!cityNo) return;
		if (!(cityData instanceof Array)) return;
		// 9112
		cityNo += "";
		for (let i = 0; i < cityData.length; i++) {
			let sheng = cityData[i];
			for (let j = 0; j < sheng.children.length; j++) {
				let shi = sheng.children[j];
				if (shi.value == cityNo) {
					// 使用return 终止循环
					return `${sheng.label}-${shi.label}`;
				}
			}
		}
	},

	

	/* 
	 * 获取url字符串参数
	 */
	getRequestParameters(locationhref) {
		let href = locationhref || "";
		let theRequest = new Object();
		let str = href.split("?")[1];
		if (str != undefined) {
			let strs = str.split("&");
			for (let i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
			}
		}
		return theRequest;
	},

	/* 
	 * 银行卡每四位空格
	 */
	formatCardNo(cardNo) {
		cardNo += "";
		return cardNo.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ');
	},

	/**
	 * 乘法，解决js精度损失问题
	 * @param {*} arg1 
	 * @param {*} arg2 
	 */
	accMul(arg1, arg2) {
		arg1 = arg1 || 0;
		var m = 0,
			s1 = arg1.toString(),
			s2 = arg2.toString();
		try {
			m += s1.split(".")[1].length
		} catch (e) {}
		try {
			m += s2.split(".")[1].length
		} catch (e) {}
		return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
	},
	
	vtTrim(src) {
		var des = this.ToCDB(src);
		des = des.replace("　"," ");
		des = des.replace("."," ");
		des = des.replace(","," ");
		des = des.replace("!"," ");
		des = des.replace("?"," ");
		des = des.replace(/\s+/g, ' ');
		return des.trim();
	},
	ToCDB(str) {
	    var tmp = "";
	    for (var i = 0; i < str.length; i++) {
	        if (str.charCodeAt(i) > 65248 && str.charCodeAt(i) < 65375) {
	            tmp += String.fromCharCode(str.charCodeAt(i) - 65248);
	        }
	        else {
	            tmp += String.fromCharCode(str.charCodeAt(i));
	        }
	    }
	    return tmp  
	},
	
	generateUUID() {
	    var d = new Date().getTime();
	    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
	        var r = (d + Math.random() * 16) % 16 | 0;
	        d = Math.floor(d / 16);
	        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
	    });
	    return uuid;
	},
	
	getUTCdate(now) {
	　　var y = now.getUTCFullYear();
	　　var m = now.getUTCMonth() + 1;
	　　var d = now.getUTCDate();
	　　return y +"-"+ (m < 10 ? "0" + m : m) + "-"+(d < 10 ? "0" + d : d) ;
	},
	
	getDateNum() {
		let time  = new Date()
		let month = time.getMonth() + 1; // 月
		let date = time.getDate(); // 日
		let hh = time.getHours(); // 时
		let mm = time.getMinutes(); // 分
		let ss = time.getSeconds(); // 秒
		if (hh >= 0 && hh < 10) { hh = '0' + hh; }
		if (mm >= 0 && mm < 10) { mm = '0' + mm; }
		if (ss >= 0 && ss < 10) { ss = '0' + ss; }
		if (month < 10) { month = '0' + month; }
		if (date < 10) { date = '0' + date; }
		return time.getFullYear().toString() + month.toString() + date.toString() + hh.toString() + mm.toString() + ss.toString();
	},
	
	
	// 选中文件之后，计算一个随机的文件名
	getRandFileName: function (filePath) {
		var extIndex = filePath.lastIndexOf('.');
		var extName = extIndex === -1 ? '' : filePath.substr(extIndex);
		return this.getDateNum() + parseInt('' + Date.now() + Math.floor(Math.random() * 900 + 100), 10).toString(36) + extName;
	},
	
	// 当前日期字符串或n天之后的日期
	getDate(data, num=0) {
		let _formt = function(date) {
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();
			month = month > 9 ? month : '0' + month;;
			day = day > 9 ? day : '0' + day;
			return `${year}-${month}-${day}`;
		}
		const newdate =data? new Date(data):new Date();
		newdate.setDate(newdate.getDate() + num);
		return _formt(newdate)
	},
	
	
	// 时间字符串格式化****-**-**
	formatTime (timeStr) {
		let y = 0
		let template = [
			'YYYY-MM-DD hh:mm:ss',
			'YYYY-MM-DD',
			'hh:mm'
		]
		let matchAry = timeStr.split("")
		let currentTemplate = matchAry.length <= 4 ? template[2] : matchAry.length > 8? template[0] : template[1]
	
		currentTemplate = currentTemplate.replace(/[a-zA-Z]/g, () => {
			let val = matchAry[y++] || '0'
		    return val
		})
		return currentTemplate
	},
	
	// 函数节流
	throttle(fn, gapTime) {
		let _nowTime = new Date().getTime()
		if (_nowTime - this._lastTime > gapTime || !this._lastTime) {
			fn()
			this._lastTime = _nowTime
		}
	},
}
